package com.changan;

import com.alibaba.druid.pool.DruidDataSource;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.changan.service.EmailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
public class test {
    //DI注入数据源
    @Autowired
    DataSource dataSource;

    @Autowired
    private EmailService emailService;

    @Test
    public void sendStringEmail() {
        emailService.sendStringEmail("1351261434@qq.com", "你好", "安哥教师节快乐！下午请你吃槟榔嘞");
        System.out.println("发送成功");
    }

    @ResponseBody
    @RequestMapping(value = "/bigEmaile", method = RequestMethod.GET)
    public String sendBigEmail() {
        File file = new File("src/main/resources/static/zong.jpg");
        emailService.sendBigEmail("mmmmmm@qq.com", "你好，美图请查收", "看我拍的咋样？", file);
        return "发送成功";
    }

    @ResponseBody
    @RequestMapping(value = "/templateEmail", method = RequestMethod.GET)
    public String sendTemplateEmail() {
        //emailService.sendTemplateEmail("mmmmmm@qq.com","激活邮件","freemarkerTemplate.html");
        emailService.sendTemplateEmail("mmmmmm@qq.com", "激活邮件", "thymeleafTemplate.html");
        return "发送成功";
    }

    @Test
    public void contextLoads() throws SQLException {
        //看一下默认数据源
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

        DruidDataSource druidDataSource = (DruidDataSource) dataSource;
        System.out.println("druidDataSource 数据源最大连接数：" + druidDataSource.getMaxActive());
        System.out.println("druidDataSource 数据源初始化连接数：" + druidDataSource.getInitialSize());

        //关闭连接
        connection.close();
    }


    @Test
    public void createBucket() {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        String accessKeyId = "LTAI5tLT3zDru7qyHtcNz6F7";
        String accessKeySecret = "GbnWEro2BGer5bQVQxp4ZtqTWOA8Ab";
        String bucketName = "t121-cuiiiii";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建存储空间。
        ossClient.createBucket(bucketName);

        // 关闭OSSClient。
        ossClient.shutdown();
    }


    @Test
    public void uploadFile() throws FileNotFoundException {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String bucketName = "<yourBucketName>";
        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        String objectName = "<yourObjectName>";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）。
        File file = new File("D:\\02.jpg");
        InputStream inputStream = new FileInputStream(file);
        ossClient.putObject(bucketName, objectName, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
}
